package de.tum.in.eist.poll.client.widgets.content;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import de.tum.in.eist.poll.client.PollService;
import de.tum.in.eist.poll.client.PollServiceAsync;
import de.tum.in.eist.poll.shared.Feedback;
import de.tum.in.eist.poll.shared.Lecture;

/**
 * factory product that creates the Feedback UI
 * @author aperture
 *
 */
public class FeedbackUI extends Composite implements Content {
	private static final PollServiceAsync pollService = GWT
			.create(PollService.class);
	private int satisfaction;

	private Lecture selectedLecture = null;
	private boolean timerDone = false;
	private Boolean rated = null;
	
	private final VerticalPanel content = new VerticalPanel();

	public FeedbackUI(Lecture selectedLecture) {
		this.selectedLecture = selectedLecture;
		
		pollService.checkRatingStatus(selectedLecture, new AsyncCallback<Boolean>() {
			
			@Override
			public void onSuccess(Boolean result) {
				rated = result;
			}
			
			@Override
			public void onFailure(Throwable caught) {
				timerDone = true;
				rated = false;
			}
		});
		
		Timer t = new Timer() {
			
			@Override
			public void run() {
				if (!timerDone){
					if (rated != null && rated == true){
						timerDone = true;
						content.clear();
						content.add(new HTML("You have already voted for this lecture."));
					}
				}
			}
		};
		t.scheduleRepeating(200);
	}

	public FeedbackUI() {
	}

	private Widget initialization() {

		content.add(new HTML("<h2>" + selectedLecture.getParentCourseTitle()
				+ ": " + selectedLecture.getTitle() + ", on: "
				+ selectedLecture.getDate() + "</h2>"));
		final FlexTable container = new FlexTable();
		final FormPanel form = new FormPanel();
		final ListBox ratingSelector = new ListBox();

		Button submitButton = new Button("Submit Feedback");

		ratingSelector.addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				satisfaction = ratingSelector.getSelectedIndex() + 1;
			}
		});

		ratingSelector.addItem("Very good");
		ratingSelector.addItem("Good");
		ratingSelector.addItem("Average");
		ratingSelector.addItem("Acceptable");
		ratingSelector.addItem("Unsatisfactory");

		final TextArea like = new TextArea();
		final TextArea dislike = new TextArea();

		container.setWidget(0, 0, new HTML(
				"How would you rate this lecture?"));
		container.setWidget(0, 1, ratingSelector);
		container.setWidget(1, 0, new HTML(
				"What did you like about the lecture?"));
		container.setWidget(1, 1, like);
		container.setWidget(2, 0, new HTML(
				"What didn't you like about the lecture?"));
		container.setWidget(2, 1, dislike);
		container.setWidget(3, 0, submitButton);

		form.add(container);

		submitButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				pollService.submitFeedback(
						new Feedback(satisfaction, like.getText(), dislike
								.getText(), selectedLecture.getNumber()),
						new AsyncCallback<Void>() {

							@Override
							public void onSuccess(Void result) {
								form.clear();
								form.add(new HTML(
										"Feedback successfully submitted!"));
							}

							@Override
							public void onFailure(Throwable caught) {
								form.clear();
								form.add(new HTML(
										"Feedback couldn't be submitted, error given:<br/>"
												+ caught.getMessage()));
							}
						});
			}
		});
		content.add(form);
		return content;
	}

	@Override
	public Widget draw() {
			initWidget(initialization());
			return this;
	}

}
